package cn.edu.kzjsxy.graduationproject.service.impl;

import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.common.TokenUtil;
import cn.edu.kzjsxy.graduationproject.dto.DormitoryDTO;
import cn.edu.kzjsxy.graduationproject.entity.Dormitories;
import cn.edu.kzjsxy.graduationproject.entity.StudentDormAssignments;
import cn.edu.kzjsxy.graduationproject.entity.Users;
import cn.edu.kzjsxy.graduationproject.mapper.DormitoriesMapper;
import cn.edu.kzjsxy.graduationproject.service.IDormitoriesService;
import cn.edu.kzjsxy.graduationproject.service.IStudentDormAssignmentsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 宿舍信息表 服务实现类
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Service
public class DormitoriesServiceImpl extends ServiceImpl<DormitoriesMapper, Dormitories> implements IDormitoriesService {

//	宿舍信息的service
	@Autowired
	private DormitoriesMapper dormitoriesMapper;

//	 学生宿舍分配关系表的service
	@Autowired
	IStudentDormAssignmentsService studentDormAssignmentsService;

	//	tokenUitl
	@Autowired
	private TokenUtil tokenUtil;

//	根据条件查询宿舍信息
	@Override
	public List<Dormitories> selectDormitoriesByCondition(Dormitories dormitories) {
		return dormitoriesMapper.selectDormitoriesByCondition(dormitories);
	}

	//根据ID获取宿舍信息
	@Override
	public DormitoryDTO selectDormitoryDTOById(String dormitoryId) {
		List<DormitoryDTO> dormitoryDTOList = dormitoriesMapper.selectDormitories(DormitoryDTO.builder().dormitoryId(dormitoryId).build());
		if(dormitoryDTOList!=null&&!dormitoryDTOList.isEmpty()){
			return  dormitoryDTOList.get(0);
		}
		return null;
	}

//分页获取宿舍信息
	@Override
	public Result<PageInfo<Dormitories>> selectDormitoriesByPage(Page page, Dormitories dormitories) {
		// 设置分页
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		// 执行查询
		List<Dormitories> dormitoriesList = dormitoriesMapper.selectDormitoriesByCondition(dormitories);
		PageInfo<Dormitories> pageInfo = new PageInfo<>(dormitoriesList);
		return Result.success(pageInfo);
	}


//	根据条件分页查询宿舍信息（楼栋 单元 宿舍 多表联查）
	@Override
	public Result<PageInfo<DormitoryDTO>> selectDormitoryDTOByPage(Page page, DormitoryDTO dormitoryDTO, HttpServletRequest request) {
		Users user = tokenUtil.resolveToken(request);
		if(Objects.equals(user.getRole(), "0")){
			dormitoryDTO.setHousemasterId(user.getUserId());
		}
		// 设置分页
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		// 执行查询
		List<DormitoryDTO> dormitoryDTOList = dormitoriesMapper.selectDormitories(dormitoryDTO);
		PageInfo<DormitoryDTO> pageInfo = new PageInfo<>(dormitoryDTOList);
//      分页查询的总数大于0
		if(pageInfo.getTotal()>0){
			List<DormitoryDTO> list = pageInfo.getList();
			for (DormitoryDTO dto : list) {
				List<StudentDormAssignments> studentDormAssignmentsList = studentDormAssignmentsService.list(new LambdaQueryWrapper<StudentDormAssignments>().eq(StudentDormAssignments::getDormitoryId, dto.getDormitoryId()));
				dto.setAssigned(studentDormAssignmentsList.size()+"/"+dto.getDormitoryBed());
			}
			pageInfo.setList(list);
		}
		return Result.success(pageInfo);
	}


//

}
